import * as Cesium from "cesium"
import { DrawerBase } from "./DrawerBase"
import type { EventsConfig } from "./DrawerBase"
import { generateBezierCurve } from "../../../utils/Curve/index"

/**
 * @description 样条曲线绘制
 */
export class BezierDrawer extends DrawerBase {
  protected events: EventsConfig = {
    cesiumEvents: [
      {
        type: "click",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          const position = this.scene.pickPosition(e.position)
          position && this.featurePositions.value.push(position)
        },
      },
      {
        type: "rightclick",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          if (this.featurePositions.value.length >= 2) {
            const position = this.scene.pickPosition(e.position)
            position && this.featurePositions.value.push(position)
            const curvePoints = generateBezierCurve(this.featurePositions.value)
            this.drawFeature({
              polyline: {
                positions: curvePoints,
              },
            })
            this.featurePositions.value = []
          }
        },
      },
    ],
  }
  protected helpers: Cesium.Entity.ConstructorOptions[] = [
    // 鼠标位置
    {
      position: new Cesium.CallbackPositionProperty(() => {
        return this.mousePosition
      }, false),
      point: {},
    },
    // 动态曲线
    {
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length >= 1) {
            const tempPoints = [
              ...this.featurePositions.value,
              this.mousePosition,
            ]
            return generateBezierCurve(tempPoints)
          }
          return undefined
        }, false),
      },
    },
  ]
}
